<?php
/**
 * set the development environment parameters
 */
if ('development' == APPLICATION_MODE) {
	// Enable all errors so we'll know when something goes wrong. 
	error_reporting(E_ALL);  
	ini_set('display_startup_errors', 1);  
	ini_set('display_errors', 1);
} else {
	// Disable all errors 
	error_reporting(0);  
	ini_set('display_startup_errors', 0);  
	ini_set('display_errors', 0);
}

/**
 * Init config
 */
$config = new Zend_Config_Ini(
      APPLICATION_PATH . '/settings/config.ini'
    , SERVER_ENV
);

Zend_Registry::set('config', $config);

/**
 * Init logs
 */
if ('development' == APPLICATION_MODE) {
	$writer = new Zend_Log_Writer_Firebug();
	$log    = new Zend_Log($writer);
	
	$writer->setPriorityStyle(8, 'TABLE');
	$log->addPriority('TABLE', 8);
	
	Zend_Registry::set('log', $log);
}

/**
 * Init MVC modules
 */
$frontController->addModuleDirectory(APPLICATION_PATH . '/modules');

/**
 * Parse modules structure
 * TODO: remove this function and replace all usage of this function
 * for now it used by Wise_Action_Controller to implement models autoloading
 */
function parseModules($dirName)
{
	return glob(APPLICATION_PATH . '/modules/*/' . $dirName);
}

// Init models directories
set_include_path(
	get_include_path() 
    . PATH_SEPARATOR . implode(PATH_SEPARATOR, parseModules('models'))
);


/**
 * Init database
 */
$config_db  = $config->db;
$cdc 		= $config_db->cache;

// init chache
$cache_db   = Zend_Cache::factory(
	  $cdc->frontendName
	, $cdc->backendName
	, $cdc->frontendOptions->toArray()
	, $cdc->backendOptions->toArray()
);

Zend_Db_Table_Abstract::setDefaultMetadataCache($cache_db);

$config_db_array = $config_db->params->toArray();
$config_db_array['driver_options'] = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8;');

try {
    $db = Zend_Db::factory($config_db->adapter, $config_db_array);
    Zend_Db_Table_Abstract::setDefaultAdapter($db);
} catch (Exception $e) {
    exit($e->getMessage());
}

if ($config_db->profiler->enabled) {
	$db_profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
	$db_profiler->setEnabled($config_db->profiler->enabled);
	$db->setProfiler($db_profiler);
}

// Set db adapter into registry
Zend_Registry::set('Zend_Db', $db);


/**
 * Init user session
 */
$currentUser = Wise_User_Session::getInstance();

/**
 * Init locale
 */

// get locale configuration
$conf_locale = $config->locale;
// set default locale
Zend_Locale::setDefault($conf_locale->default);
// cache locale data
$clc = $conf_locale->cache;
$locale_cache = Zend_Cache::factory(
	  $clc->frontendName
	, $clc->backendName
	, $clc->frontendOptions->toArray()
	, $clc->backendOptions->toArray()
);
Zend_Locale::setCache($locale_cache);

// initiate locale
try {
	$locale = new Zend_Locale($currentUser->locale ? $currentUser->locale : Zend_Locale::BROWSER);
} catch (Zend_Locale_Exception $e) {
	$locale = new Zend_Locale('en'); // assign default locale in case autodetection is filed
}

// set locale into registry so make it available from models and controllers
Zend_Registry::set('Zend_Locale', $locale);

/**
 * Init i18n
 */
// initialize translation cache
$ctc = $config->internationalization->cache;
$cache_translate = Zend_Cache::factory(
	  $ctc->frontendName
	, $ctc->backendName
	, $ctc->frontendOptions->toArray()
	, $ctc->backendOptions->toArray()
);
Zend_Translate::setCache($cache_translate);

// get locale
$current_lang = $locale->getLanguage();

if ('development' == APPLICATION_MODE) {

	$options = array(
		  'logUntranslated' => true
		, 'log' => Zend_Registry::get('log')
	);

} else {
	$options = array();
}

// Init translation
// QUICK FIX: $translate = new Zend_Translate('tmx', $this->_root . '/languages');
// we need to pass current locale directly
$translate = new Zend_Translate('tmx', realpath(APPLICATION_PATH . '/../') . '/languages/general.tmx', $locale, $options);

if (!$translate->isAvailable($current_lang)) { 
    $locale->setLocale('en');
	$translate->setLocale($locale); 
}

// save locale state into user session
$currentUser->locale = $locale->getLanguage();

Zend_Registry::set('Zend_Translate', $translate);


/**
 * Init routers
 */
$router = $frontController->getRouter();

$router->addRoute(
    'translation',
    new Zend_Controller_Router_Route(
	    'translation/search'
	    , array('module' => 'translation'
	    ,       'controller' => 'search'
	    ,	    'action' => 'index'
	    )
	)
);

$router->addRoute(
    'translation_search',
    new Zend_Controller_Router_Route(
    	'translation/search/:keyword'
    	, array('module' => 'translation'
	    	, 'controller' => 'search'
	    	, 'action' => 'result'
	    )
    )
);

$router->addRoute(
    'translation_discussion',
    new Zend_Controller_Router_Route(
    	'translation/discussion/:id'
    	, array('module' => 'translation'
	    	, 'controller' => 'discussion'
	    	, 'action' => 'view'
	    )
	)
);

/**
 * Init view
 */

// Bootstrap layouts and get view object
$view = new Wise_View();
$view->User = $currentUser;
Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer($view));

Zend_Layout::startMvc(array(
      'layoutPath' => APPLICATION_PATH . '/layouts'
	, 'layout' => 'main'
));